<?php
namespace Admin\Model;

use Common\Model\CommonModel;

class ExamineModel extends CommonModel {
	protected $softdelete = false;
	protected $deleteTime = 'delete_time';
	
    public function get_list($where = array(),$order = '',$limit = ''){
       $list =  $this->where($where)->select();
        return $list;
    }

    public function get_data_one($where){
        $data = $this->where($where)->find();
        return $data;
    }

    /**
     * 开始审核流程
     * @param  [type] $recharge [description]
     * @param  [type] $type     [description]
     * @return [type]           [description]
     */
    public function to_examine($recharge, $type){
        $steps = M("ExamineStep")->where(array('type'=>$type))->select();
        $examines = array();
        if($steps) foreach ($steps as $_key => $_row) {
            $step_row = $_row;
            unset($step_row['id']);
            $step_row['relate_id'] = $recharge['id'];
            
            if ($_key == 0) {
                $step_row['admin_id'] = session('ADMIN_ID');
                $step_row['status'] = 1;
                $step_row['desc'] = $recharge['desc'];
            }else{
                $step_row['admin_id'] = 0;
                $step_row['status'] = 0;
                $step_row['desc'] = '';
            }

            $step_row['create_time'] = time();
            // $step_row['update_time'] = time();

            $examines[] = $step_row;
        }
        $flag = $this->addAll($examines);
        return $flag;
        // 获取对应类型的审核流程；
        // 创建审核流程组
    }

    /**
     * 执行审核操作
     * @param  [type] $examine_id 审核单id
     * @param  [type] $status     执行状态
     * @return [type]             [description]
     */
    public function do_examine($examine_id, $status, $desc){
        if (!in_array($status, array(2,3))) {
            // 状态出错
            return -1;
        }
        
        $examine_row = $this->where(array('id'=>$examine_id))->find();

        $relate_id = $examine_row['relate_id']; // 关联id，可以是上级物流公司或者上级消费机构
        $model = $examine_row['type'] == 'logistics_recharge'?'1':'3';         // 关联机构类型，1：上级物流公司，3：上级消费机构
        $next_step = intval($examine_row['step']) + 1;  // 下一步审核操作
        if (empty($examine_row)) {
            // 未找到审核单
            return -2;
        }elseif (in_array($examine_row['status'], array(2,3))){
            // 此审核单已审核过
            return -3;
        }

        /*审核成功逻辑*/
        if ($status == 2) {

            /*当前步骤审核*/
            $examine_row_update = array(
                'admin_id' => session('ADMIN_ID'),
                'status' => $status,
                'desc' => $desc,
                'update_time' => time(),
            );
            $flag = $this->where(array('id'=>$examine_id))->save($examine_row_update);

            if (!$flag) {
                // 当前审核单保存失败
                return -4;
            }

            $next_step_row = $this->where(array('step'=> $next_step, 'relate_id'=>$relate_id) )->find();
            if (!empty($next_step_row)) {   /*如果有下一步，通知下一步进行审核*/

                /*下一步步骤开启审核*/
                $next_step_row_update = array(
                    'status' => 1,
                    'desc' => '',
                );
                $result = $this->where(array('id'=>$next_step_row['id']))->save($next_step_row_update);

                if (!$result) {
                    // 下一步审核开启失败
                    return -5;
                }

            }else{ /*如果没有下一步，则进行充值*/
                
                $recharge_row = D("Admin/Recharge")->where(array('id'=>$relate_id))->find();
                $result = D("Admin/Recharge")->insert_data($recharge_row, $model, $status);
                if ($result < 0) {
                    // 充值单状态更新失败
                    return -6;
                }
            }
        }

        /*审核失败逻辑*/
        elseif ($status == 3) {
            /*当前步骤审核*/
            $examine_row_update = array(
                'admin_id' => session('ADMIN_ID'),
                'status' => $status,
                'desc' => $desc,
                'update_time' => time(),
            );
            $flag = $this->where(array('id'=>$examine_id))->save($examine_row_update);

            if (!$flag) {
                // 当前审核单保存失败
                return -4;
            }

            /*关闭充值单*/
            $recharge_update = array(
                'status' => 0
            );
            $result = D("Admin/Recharge")->where(array('id'=>$relate_id))->save($recharge_update);
            if ($result < 0) {
                // 充值单状态更新失败
                return -6;
            }
        }

        return $result;
    }

}